package com.walkline.util.network;

import net.rim.device.api.system.EncodedImage;

import com.walkline.util.Function;
import com.walkline.util.ui.StatusListStyleButtonField;
import com.walkline.weibo.WeiboSDK;

/**
 * 下载微博配图图片
 */
public class DownloadStatusImages implements Runnable
{
	private WeiboSDK _weibo;
	private StatusListStyleButtonField _item;
	private int _sequence = 0;
	private boolean _is_retweeted = false;

	public DownloadStatusImages(WeiboSDK weibo, StatusListStyleButtonField item, int sequence, boolean isRetweeted)
	{
		_weibo = weibo;
		_item = item;
		_sequence = sequence;
		_is_retweeted = isRetweeted;
	}

	public void run()
	{
		if (_item.getAvatarUrl().equals("")) {return;}

		byte[] data = _weibo.doRequestRAW(_is_retweeted ? _item.getRetweetedImagesUrl()[_sequence] : _item.getStatusImagesUrl()[_sequence]);

		if (data != null)
		{
			try {
				EncodedImage image = EncodedImage.createEncodedImage(data, 0, -1);

				if (_is_retweeted)
				{
					_item.setRetweetedImage(data, _sequence);
				} else {
					_item.setStatusImage(data, _sequence);	
				}
			} catch (Exception e) {}
		}
	}
}